package com.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MatchDegree {
	public static Gson g = new Gson();
	public static double wordMatch(String s1, String s2){
		double res=0.0;
		if(s1==null || s2==null || s1.trim().equals("")){
			return 0.0;
		}
		String[] arr1 = s1.split(" ");
		String[] arr2 = s2.split(" ");
		List<String> list = new ArrayList<String>();
		for(int i=0;i<arr1.length;i++){
			if(arr1[i].length()>0 && !arr1[i].equals(" ")){
				list.add(arr1[i]);
			}
		}
		if(list.size()==0){
			return 0.0;
		}
		int s=0;
		for(int i=0;i<arr2.length;i++){
			if(list.indexOf(arr2[i])>=0){
				s++;
			}
		}
		res = s*1.0/list.size();
		return res;
	}
	public static double wordsMatch(String s1, String s2){
		double res = 0.0;
		if(s1==null || s2==null || s1.trim().equals("")){
			return 0.0;
		}
		String[] arr1 = s1.split(" ");
		String[] arr2 = s2.split(" ");
		if(arr1.length<2 || arr2.length<2){
			return 0.0;
		}
		List<String> list = new ArrayList<String>();
		for(int i=0;i<arr1.length-1;i++){
			list.add(arr1[i]+" "+arr1[i+1]);
		}
		int s=0;
		for(int i=0;i<arr2.length-1;i++){
			if(list.indexOf(arr2[i]+" "+arr2[i+1])>=0){
				s++;
			}
		}
		res = s*1.0/list.size();
		return res;
	}
	public static double startMatch(String s1, String s2){
		double res = 0.0;
		
		return res;
	}
	public static void testANZ(){
		String gmt = "Japan";
		File f1 = new File("C:/crystalData/"+gmt+"/noCompany.txt");
		try {
			BufferedReader br = new BufferedReader(new FileReader(f1));
			String str;
			int s = 0;
			while((str=br.readLine())!=null){
				if(str.equals("DR.CI:LABO CO., LTD.")){
					s=1;
				}
				if(s==0){
					continue;
				}
				if(str.indexOf("/")>0 || str.indexOf("?")>0 || str.indexOf("*")>0 || str.indexOf(":")>0){
					continue;
				}
				File f2 = new File("C:/crystalData/"+gmt+"/"+str+"/companies.txt");
				BufferedReader reader = new BufferedReader(new FileReader(f2));
				String companies = reader.readLine();
				reader.close();
				List<Map<String,Object>> compList = new ArrayList<Map<String,Object>>();
				compList = g.fromJson(companies, new TypeToken<List<Map<String,Object>>>(){}.getType());
//				System.out.println(compList.size());
				if(compList.size()==0){
					System.out.println(str);
					continue;
				}
				List<Double> list = new ArrayList<Double>();
				int max=0;
				for(int i=0;i<compList.size();i++){
					String tempName = compList.get(i).get("name").toString();
					list.add(wordMatch(str.toUpperCase(), tempName.toUpperCase())*0.4+wordsMatch(str.toUpperCase(), tempName.toUpperCase())*0.6);
					if(i>0 && list.get(i)>list.get(i-1)){
						max=i;
					}
				}
//				System.out.println(compList.get(max).get("name"));
//				System.out.println(str);
//				System.out.println(list.get(max));
				int type=5;
				File a = new File("C:/crystalData/"+gmt+"/a"+type+".txt");
				File b = new File("C:/crystalData/"+gmt+"/b"+type+".txt");
				BufferedWriter bwa = new BufferedWriter(new FileWriter(a,true));
				BufferedWriter bwb = new BufferedWriter(new FileWriter(b,true));
				if(list.get(max)>type*1.0/10){
					String id = compList.get(max).get("companyId").toString();
					bwa.append(str);
					bwa.append("\n");
					File idFile = new File("C:/crystalData/"+gmt+"/"+str+"/"+Math.round(Double.parseDouble(id))+".id");
					File old_idFile = new File("C:/crystalData/"+gmt+"/"+str+"/"+id+".id");
					old_idFile.delete();
					idFile.createNewFile();
					BufferedWriter bw = new BufferedWriter(new FileWriter(idFile));
					bw.write(compList.get(max).get("name").toString());
					bw.close();
					System.out.println(compList.get(max).get("name"));
				}else{
					bwb.append(str);
					bwb.append("\n");
				}
				bwa.close();
				bwb.close();
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void test(){
		File f = new File("C:/crystalData/ANZ/ACT DEPARTMENT OF HEALTH");
		File[] files = f.listFiles();
		for (File file : files) {
			System.out.println(file.getName());
		}
	}
	public static void main(String[] args) {
		String s1 = "i am a boy";
		String s2 = "you are a boy";
//		System.out.println(MatchDegree.wordsMatch(s1, s2));
		testANZ();
//		System.out.println("a*b/c.d".indexOf("."));
//		test();
	}
}
